package com.tsd.system.service.impl;

import com.tsd.common.vo.BatchDeleteLogicVo;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysUserOrgRightMapper;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserOrgRight;
import com.tsd.system.service.SysUserOrgRightService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysUserOrgRight 服务实现层
 *
 * @author 15971
 * @date 2019/12/30 16:10:02
 */
@Service
public class SysUserOrgRightServiceImpl extends BaseServiceImpl implements SysUserOrgRightService {

    @Resource
    private SysUserOrgRightMapper sysUserOrgRightMapper;

    @Override
    public SysUserOrgRight getSysUserOrgRightById(Long id) throws Exception {
        return sysUserOrgRightMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysUserOrgRight queryBySid(String sid) throws Exception {
        return sysUserOrgRightMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysUserOrgRight(Long id, SysUser opUser) throws Exception {
        sysUserOrgRightMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void batchDeleteSysUserOrgRight(String userSid) throws Exception {
        List<SysUserOrgRight> old = findSysUserOrgRightByUserSid(userSid);
        List<Long> delIds = new ArrayList<Long>();
        for (SysUserOrgRight sysUserOrgRight : old) {
            delIds.add(sysUserOrgRight.getId());
        }
        if (!delIds.isEmpty()) {
            for (Long delId : delIds) {
                deleteSysUserOrgRight(delId, null);
            }
        }

    }

    @Override
    public void saveSysUserOrgRight(SysUserOrgRight record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            record.setCreate_date(new Date());
            super.checkEmpty(record.getOrg_sid(), "单位不能为空");
            super.checkEmpty(record.getUser_sid(), "用户不能为空");
            if (opUser != null) {
                record.setCreator_name(opUser.getName());
                record.setCreator_sid(opUser.getSid());
            }
            if (record.getSn() == null) {
                record.setSn(1);
            }
            record.setCreate_date(new Date());
            record.setSid(super.getPrimaryKey());
            record.setState(1);
            sysUserOrgRightMapper.insertSelective(record);
        } else {
            record.setUpdate_date(new Date());
            if (opUser != null) {
                record.setUpdator_name(opUser.getName());
                record.setUpdator_sid(opUser.getSid());
            }
            sysUserOrgRightMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void batchSaveSysUserOrgRight(String user_sid, List<SysUserOrgRight> list, SysUser opUser) throws Exception {
        List<SysUserOrgRight> oldList = this.findSysUserOrgRightByUserSid(user_sid);
        List<Long> existIdList = new ArrayList<>();
        List<SysUserOrgRight> insertList = new ArrayList<>();
        List<SysUserOrgRight> updateList = new ArrayList<>();
        for (SysUserOrgRight right : list) {
            SysUserOrgRight oldData = ListUtil.get(oldList, t -> right.getOrg_sid().equals(t.getOrg_sid()));
            if (oldData != null) {
                right.setId(oldData.getId());
                updateList.add(right);
                existIdList.add(oldData.getId());
            } else {
                insertList.add(right);
            }
        }

        List<SysUserOrgRight> delList = ListUtil.filter(oldList, t -> !existIdList.contains(t.getId()));
        if (!HlpUtils.isEmptyList(delList)) {
            List<Long> deleteIdList = ListUtil.map(delList, SysUserOrgRight::getId);
            if (!HlpUtils.isEmptyList(deleteIdList)) {
                BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo();
                logicVo.setIdList(deleteIdList);
                sysUserOrgRightMapper.deleteByIdList(logicVo);
            }
        }

        if (!HlpUtils.isEmptyList(insertList)) {
            List<SysUserOrgRight> extList = new ArrayList<>();
            for (SysUserOrgRight right : insertList) {
                super.autoInjectBaseData(right, opUser, this.TYPE_CREATE);
                extList.add(right);
                if (extList.size() == 200) {
                    sysUserOrgRightMapper.insertList(extList);
                    extList.clear();
                }
            }
            if (!HlpUtils.isEmptyList(extList)) {
                sysUserOrgRightMapper.insertList(extList);
            }
        }

        if (!HlpUtils.isEmptyList(updateList)) {
            List<SysUserOrgRight> extList = new ArrayList<>();
            for (SysUserOrgRight right : updateList) {
                super.autoInjectBaseData(right, opUser, this.TYPE_UPDATE);
                extList.add(right);
                if (extList.size() == 200) {
                    sysUserOrgRightMapper.updateList(extList);
                    extList.clear();
                }
            }
            if (!HlpUtils.isEmptyList(extList)) {
                sysUserOrgRightMapper.updateList(extList);
            }
        }
    }

    @Override
    public List<SysUserOrgRight> findSysUserOrgRights(Map<String, Object> params, PageBean page) throws Exception {
        return sysUserOrgRightMapper.selectByPage(params, page);
    }

    @Override
    public List<SysUserOrgRight> findSysUserOrgRights(Map<String, Object> params) throws Exception {
        return sysUserOrgRightMapper.selectByParams(params);
    }

    @Override
    public List<SysUserOrgRight> findSysUserOrgRightByUserSid(String user_sid) throws Exception {
        return sysUserOrgRightMapper.selectByUserSid(user_sid);
    }
}
